'use strict';
const Controller = require('./base');
class ArticleController extends Controller {

  // 列表
  async list() {
    const { ctx, service } = this;
    const page = ctx.query.page;
    const pageSize = 10;
    const data = await service.article.list(page, pageSize);
    this.success(data);
  }

  // 根据栏目id查
  async findCid() {
    const { ctx, service } = this;
    const cat_id = ctx.query.cat_id;
    const data = await service.article.findCid(cat_id);
    this.success(data);
  }

  // 详情
  async detail() {
    const { ctx, service } = this;
    const id = ctx.query.id;
    const detail = await service.article.findId(id);
    this.success(detail);
  }

  // 添加
  async add() {
    const { ctx, service } = this;
    const query = ctx.request.body;
    const article = await service.article.create(query);
    this.success(article);
  }

  // 更新
  async update() {
    const { ctx, service } = this;
    const result = await service.article.update({ ...ctx.request.body });
    this.success(result);
  }

  async del() {
    const { ctx, service } = this;
    const id = ctx.query.id;
    const result = await service.article.delete(id);
    this.success(result);
  }
}

module.exports = ArticleController;
